package com.kukudexin.time.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: 酷酷的鑫
 * @Date: 2021/8/6 13:45
 * @description:
 */
public class Test02 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "wkcto", "good", "hello", "jj", "xx", "jj", "jj","bjpowernode","xx", "good");
        //1)通过List集合获得Stream流
        Stream<String> stream = list.stream();

//        stream.distinct().forEach(System.out::println);
//        System.out.println("-----------------------");
//
//        list.stream().filter(x->x.length() > 4).forEach(System.out::println);
//        System.out.println("-----------------------");

        //sort()排序
//        list.stream().sorted(String::compareTo).forEach(System.out::println);
//        System.out.println("-----------------------");

        //sort()排序
//        list.stream().sorted(String::compareTo).forEach(System.out::println);
//        System.out.println("-----------------------");

        //limt()截断操作
//        list.stream().limit(5).forEach(System.out::println);
//        System.out.println("-----------------------");

        //skip()跳过
//        list.stream().skip(3).forEach(System.out::println);
//        System.out.println("-----------------------");

//        list.stream()
//                .map(String::toUpperCase)   //为每个元素应用toUpperCase()把小写转换为大写
//                .forEach(System.out::println);
//        System.out.println("-----------------------");

//        list.stream()
//                .map(String::length)
//                .map(len -> len + "  ")
//                .forEach(System.out::print);
//        System.out.println();
//        System.out.println("-----------------------");
//
//        list.stream().map(a -> {
//            return a.length();
//        }).map(len -> {
//            return len + "  ";
//        }).forEach(a -> System.out.print(a));
//        System.out.println();
//        System.out.println("-----------------------");

        //转换为数值流
//        List<Integer> integerList = Arrays.asList(54,1,78,90,345);
//        IntStream intStream = integerList.stream().mapToInt(x -> x);
//        intStream.forEach(x -> System.out.print( x + " "));
//        System.out.println();
//        System.out.println("-----------------------");

//        System.out.println( list.stream().allMatch(s -> s.length() > 3));
//        System.out.println( list.stream().anyMatch(s -> s.equals("wkcto")));
//        System.out.println( list.stream().noneMatch(s -> s.equals("jj")));
//        System.out.println(list.stream().filter(s -> s.length() > 10).findAny().get() );
//        System.out.println( list.stream().filter(s -> s.length() > 20).findFirst().orElse("不存在"));
//        list.stream().forEach(System.out::println);

//        System.out.println(list.stream().filter(x -> x.length() > 2).count());

        Optional<String> reduce = list.stream().reduce((s1, s2) -> s1 + "--" + s2);
//        reduce.ifPresent(System.out::println);

        List<Integer> list2 = Arrays.asList(6,21,87,34,1,78,54);
        System.out.println(list2.stream().reduce(0, Integer::sum) );
//        System.out.println(list2.stream().reduce((x,y)->x+y).orElse(0) );
//        System.out.println(list2.stream().reduce(Math::max).get() );

//        System.out.println(list2.stream().mapToInt(x->x).sum() );
//        System.out.println(list2.stream().mapToInt(x->x).max().getAsInt());

        Set<String> stringSet = list.stream().collect(Collectors.toSet());
//        System.out.println(stringSet);
//        Object[] objects = list.toArray();
//        System.out.println( Arrays.toString(objects));
//
//        String[] toArray = list.stream().toArray(String[]::new);
//        System.out.println(Arrays.asList(toArray));
        String collect = list.stream().collect(Collectors.joining(","));
        System.out.println(collect);

    }
}
